﻿Imports System.Data
Imports System.Data.SqlClient

Public Class frmBackupAndRestore
    Private connect As ConnectDB
    Private sqlstr As String

    Private Sub frmBackupAndRestore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If rdPhucHoi.Checked = True Then
            GroupPanel1.Enabled = True
        ElseIf rdSaoLuu.Checked = True Then
            GroupPanel3.Enabled = True
        End If
    End Sub
#Region "Xử lý giao diện"
    Private Sub btnChonFileBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonFileBackup.Click
        Dim diag As New OpenFileDialog
        With diag
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtChonFileBackup.Text = .FileName
            End If
        End With
    End Sub

    Private Sub btnBoPhucHoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBoPhucHoi.Click
        txtChonFileBackup.Text = ""
    End Sub


    Private Sub btnChonLuuFileBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonLuuFileBackup.Click
        Dim folder As New FolderBrowserDialog()
        With folder
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtLuuFileBackup.Text = .SelectedPath
            End If
        End With
    End Sub

    Private Sub btnBiSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBiSaoLuu.Click
        txtTenFileBackup.Text = ""
        txtLuuFileBackup.Text = ""
    End Sub


    Private Sub rdSaoLuu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdSaoLuu.CheckedChanged, rdPhucHoi.CheckedChanged
        If rdSaoLuu.Checked = True Then
            GroupPanel3.Enabled = True
            GroupPanel1.Enabled = False
        Else
            GroupPanel3.Enabled = False
            GroupPanel1.Enabled = True
        End If
    End Sub
#End Region

#Region "Xử lý sao lưu phục hồi dữ liệu"
    Private Sub btnSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaoLuu.Click
        If txtLuuFileBackup.Text = "" Then
            MessageBox.Show("Bạn phải chọn nơi lưu file!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtLuuFileBackup.Focus()
            Exit Sub
        ElseIf txtTenFileBackup.Text = "" Then
            MessageBox.Show("Bạn phải đặt tên cho file muốn lưu trữ!.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtTenFileBackup.Focus()
            Exit Sub
        End If
        Dim strsql As String = txtLuuFileBackup.Text + "\" + txtTenFileBackup.Text + ".bak"
        BackUp(strsql)

    End Sub

    Public Sub BackUp(ByVal Path As String)
        Try
            connect = New ConnectDB() 'BACKUP DATABASE QLNhaTro
            'TO DISK = 'D:\DBBackup\QLNhaTro.bak'  
            'WITH INIT, COMPRESSION
            ' BACKUP DATABASE QLSV TO DISK = 'D:\test.bak' WITH FORMAT;
            sqlstr = "BACKUP DATABASE QLNhaTro TO DISK = '" & Path & "' WITH FORMAT"
            connect.Execute(sqlstr)
            MessageBox.Show("Sao lưu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub

    Private Sub btnPhucHoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPhucHoi.Click
        If txtChonFileBackup.Text = "" Then
            MessageBox.Show("Bạn phải chọn file dữ liệu muốn phục hồi!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtChonFileBackup.Focus()
            Exit Sub
        End If
        If txtTenDatabase.Text = "" Then
            MessageBox.Show("Bạn phải điền tên dữ liệu muốn phục hồi!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtTenDatabase.Focus()
            Exit Sub
        End If
        ReStore(txtChonFileBackup.Text)
        txtChonFileBackup.Text = ""
        txtTenDatabase.Text = ""
    End Sub
    Public Sub ReStore(ByVal Path As String)
        Try
            connect = New ConnectDB()  'RESTORE FILELISTONLY FROM  DISK = N'D:\Backup\SomeDB.bak'
            sqlstr = "RESTORE HEADERONLY FROM  DISK = N'" & Path & "'"
            'sqlstr = "RESTORE DATABASE " & txtTenDatabase.Text & " FROM  DISK = N'" & Path & "' with replace"
            connect.Execute(sqlstr)
            MessageBox.Show("Phục hồi dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

#End Region
End Class